package com.qijian.maindata.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.common.utils.uuid.IdUtils;
import com.qijian.maindata.domain.Team;
import com.qijian.maindata.domain.query.TeamQuery;
import com.qijian.maindata.domain.vo.TeamVo;
import com.qijian.maindata.service.ITeamService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 生产班组Controller
 *
 * @author qijian
 * @date 2025-04-01
 */
@Api(tags = "生产班组")
@RestController
@RequiredArgsConstructor
@RequestMapping("/maindata/team")
public class TeamController extends BaseController {

    private final ITeamService teamService;

    /**
     * 查询生产班组列表
     */
    @GetMapping("/list2")
    public TableDataInfo list2(TeamQuery query, Page<Team> page) {
        initOrderItem(Team.class, page.orders());
        return getDataTable(teamService.page(page, WhereEntityTool.invoke(query)));
    }

    /**
     * 查询生产班组列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TeamQuery query, Page<TeamVo> page) {
        return getDataTable(teamService.pageVo(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /**
     * 导出生产班组列表
     */
    @Log(title = "生产班组" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(TeamQuery query, HttpServletResponse response) {
        new ExcelUtil<>(Team. class).exportExcel(response,teamService.list( WhereEntityTool.invoke(query)), "生产班组数据");
    }

    /**
     * 获取生产班组详细信息
     */
    @GetMapping(value = "/{teamId}")
    public AjaxResult getInfo(@PathVariable("teamId") Long teamId) {
        TeamVo teamVo = teamService.getInfoById(teamId);
        return AjaxResult.success(teamVo);
    }

    /**
     * 新增生产班组
     */
    @Log(title = "生产班组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Team team) {
        team.setTeamCode("BZ-" + String.format("%04d", teamService.count() + 1));
        team.setTeamId(IdUtils.singletonSnowId());
        return toAjax(teamService.save(team));
    }

    /**
     * 修改生产班组
     */
    @Log(title = "生产班组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Team team) {
        return toAjax(teamService.update(team));
    }

    /**
     * 删除生产班组
     */
    @Log(title = "生产班组" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{teamIds}")
    public AjaxResult remove(@PathVariable List<Long> teamIds) {
        teamIds.forEach(id -> approval(teamService.getById(id)));
        return toAjax(teamService.removeByIds(teamIds));
    }
}
